package info.javafans.dao;

import info.javafans.database.DatabaseManager;
import info.javafans.domain.User;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class UserDao {
	
	DatabaseManager dbManager = new DatabaseManager();
	
	/**
	 * 查找所有用户
	 * @return
	 */
	public List<User> selectAllUsers() {

		List<User> list = new ArrayList<User>();
		Connection conn = dbManager.getConnection();
		try {
			Statement stmt = conn.createStatement();
			String sql = "select * from jb_users";
			ResultSet result = stmt.executeQuery(sql);

			while (result.next()) {
				User user = new User();
				
				user.setId(""+result.getInt("id"));
				user.setUsername(result.getString("username"));
				user.setPassword(result.getString("password"));
				user.setNickname(result.getString("nickname"));
				user.setEmail(result.getString("email"));
				user.setRegistered(result.getString("registered").substring(0, 19));
				user.setStatus(result.getInt("status"));

				list.add(user);
			}

		} catch (SQLException e) {
			System.out
					.println("info.javafans: UserDao-selectAllUser-execute query");
			// e.printStackTrace();
		} finally {
			try {
				conn.close();
				return list;
			} catch (SQLException e) {
				System.out
						.println("info.javafans:  UserDao-selectAllUser");
				// e.printStackTrace();
			}
		}

		return null;
	}

	
	/**
	 * 按id查找用户
	 * 
	 * @param userId
	 */
	public User selectUserById(String userId) {

		User user = new User();
		Connection conn = dbManager.getConnection();
		try {
			Statement stmt = conn.createStatement();
			String sql = "select * from jb_users where id = " + userId;
			ResultSet result = stmt.executeQuery(sql);

			if (result.next()) {
				user.setId(""+result.getInt("id"));
				user.setUsername(result.getString("username"));
				user.setPassword(result.getString("password"));
				user.setNickname(result.getString("nickname"));
				user.setEmail(result.getString("email"));
				user.setRegistered(result.getString("registered"));
				user.setStatus(result.getInt("status"));
			}

		} catch (SQLException e) {
			System.out
					.println("info.javafans: UserDao-selectUserById-execute query");
			// e.printStackTrace();
		} finally {
			try {
				conn.close();
				return user;
			} catch (SQLException e) {
				System.out
						.println("info.javafans: UserDao-selectUserById-close conn");
				// e.printStackTrace();
			}
		}

		return null;
	}
	
	/**
	 * 添加User
	 * @param u
	 * @return
	 */
	public boolean addUser(User u) {
		Connection conn = dbManager.getConnection();
		try {
			Statement stmt = conn.createStatement();
			String sql = "insert into jb_users(username,password,nickname,email,registered,status) values('"+u.getUsername()+"','" +
					u.getPassword()+"','"+u.getNickname()+"','"+u.getEmail()+"','"+u.getRegistered()+"',"+u.getStatus()+")";
			
			int result = stmt.executeUpdate(sql);

			if (result == 1) {
				return true;
			}
			else {
				return false;
			}

		} catch (SQLException e) {
			System.out
					.println("info.javafans: CategoryDao-addUser-execute query");
			// e.printStackTrace();
		} finally {
			try {
				if (conn!=null) {
					conn.close();
				}
			} catch (SQLException e) {
				System.out
						.println("info.javafans: CategoryDao-addUser-close conn");
				// e.printStackTrace();
			}
		}

		return false;
	}
	
	/**
	 * 更新User
	 * @param u
	 * @return
	 */
	public boolean editUser(User u) {
		Connection conn = dbManager.getConnection();
		try {
			Statement stmt = conn.createStatement();
			String sql = "update jb_users set username='"+u.getUsername()+"',nickname='"+
			u.getNickname()+"',email='"+u.getEmail()+"',status="+u.getStatus()+" where id="+u.getId();
			
			int result = stmt.executeUpdate(sql);

			if (result == 1) {
				return true;
			}
			else {
				return false;
			}

		} catch (SQLException e) {
			System.out
					.println("info.javafans: CategoryDao-editUser-execute query");
			// e.printStackTrace();
		} finally {
			try {
				if (conn!=null) {
					conn.close();
				}
			} catch (SQLException e) {
				System.out
						.println("info.javafans: CategoryDao-editUser-close conn");
				// e.printStackTrace();
			}
		}

		return false;
	}
	
	/**
	 * 删除User
	 * @param u
	 * @return
	 */
	public boolean deleteUser(String id) {
		Connection conn = dbManager.getConnection();
		try {
			Statement stmt = conn.createStatement();
			String sql = "delete from jb_users where id="+id;
			int result = stmt.executeUpdate(sql);

			if (result == 1) {
				return true;
			}
			else {
				return false;
			}

		} catch (SQLException e) {
			System.out
					.println("info.javafans: CategoryDao-deleteUser-execute query");
			// e.printStackTrace();
		} finally {
			try {
				if (conn!=null) {
					conn.close();
				}
			} catch (SQLException e) {
				System.out
						.println("info.javafans: CategoryDao-deleteUser-close conn");
				// e.printStackTrace();
			}
		}

		return false;
	}
	
	/**
	 * 按id查找用户
	 * 
	 * @param userId
	 */
	public User UserLogin(String username, String password) {

		User user = null;
		Connection conn = dbManager.getConnection();
		try {
			Statement stmt = conn.createStatement();
			String sql = "select * from jb_users where username = '" + username + "' and password = '"+password+"'";
			ResultSet result = stmt.executeQuery(sql);

			if (result.next()) {
				user = new User();
				user.setId(""+result.getInt("id"));
				user.setUsername(result.getString("username"));
				user.setPassword(result.getString("password"));
				user.setNickname(result.getString("nickname"));
				user.setEmail(result.getString("email"));
				user.setRegistered(result.getString("registered"));
			}

		} catch (SQLException e) {
			System.out
					.println("info.javafans: UserDao-selectUserById-execute query");
			// e.printStackTrace();
		} finally {
			try {
				conn.close();
				return user;
			} catch (SQLException e) {
				System.out
						.println("info.javafans: UserDao-selectUserById-close conn");
				// e.printStackTrace();
			}
		}

		return null;
	}
}
